package com.city.parking.provider;

import com.city.parking.provider.ParkingProviderHelper.Car;
import com.city.parking.provider.ParkingProviderHelper.Parking;
import com.city.parking.util.Utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "parking.db";
    private static final int DATABASE_VERSION = 2;
    public static final String TABLE_PARKINGS = "parkings";
    public static final String TABLE_CARS = "cars";

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        log("onCreate");
        onUpgrade(db, 0, DATABASE_VERSION);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int curVersion) {
        // TODO Auto-generated method stub
    	if (oldVersion <  2) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_PARKINGS + " ;");
    	}
    	
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_PARKINGS + " ("
                + Parking._ID + " INTEGER PRIMARY KEY, "
                + Parking.COLUMN_OWN + " INTEGER DEFAULT 0, "
                + Parking.COLUMN_LONGITUDE + " REAL DEFAULT 0, "
                + Parking.COLUMN_LATITUDE + " REAL DEFAULT 0, "
                + Parking.COLUMN_ADDR + " TEXT DEFAULT NULL, "
                + Parking.COLUMN_NAME + " TEXT DEFAULT NULL);");
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_CARS + " (" + Car._ID
                + " INTEGER PRIMARY KEY, " + Car.COLUMN_OWNER
                + " TEXT DEFAULT NULL, " + Car.COLUMN_ISDEFAULT
                + " INTEGER DEFAULT 0, " + Car.COLUMN_ISBIG
                + " INTEGER DEFAULT 0, " + Car.COLUMN_NO
                + " TEXT DEFAULT NULL);");
    }

    private void log(String msg) {
        Utils.log("ParkingDBHelper " + msg);
    }
}
